package ko;

import java.lang.Thread.State;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class KEngine extends Activity {
	public KThread thread;

	public Boolean paused = false;
	public final float maxElapsed;
	public final short maxFrameSkip = 5;

	private long _rate;
	
	private float _last;
	private float _flashTime;

	public KEngine(int w, int h, long frameRate, int color) {
		K.W = w;
		K.H = h;
		
		K.FrameRate = frameRate;
		K.BackColor = color;
		
		this.maxElapsed = 1000.0f / K.FrameRate / 1000.0f;// and

		K.engine = this;
		K.game = new KGame();
		
		K.bitmap = Bitmap.createBitmap(K.W, K.H, Config.ARGB_8888);
		K.render = new Canvas(K.bitmap);
		K.matrix = new Matrix();
		
		K.paint = new Paint();
		//K.paint.setAntiAlias(false);
		K.paint.setColor(K.BackColor);
		K.paint.setDither(true);
		
		
		KTweener.init();

		this._rate = (long) (1000.0f / K.FrameRate);
		this._last = 0.0f;
		K._timeFrame = 0.0f;
		
		thread = new KThread();
		
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initWindow();
		K.stage = new StageView(this);
		KInput.enable(K.stage);
		//KInput.enable(this);
		

		Log.i("KEngine", "onCreate");
		
		setContentView(K.stage);
		
		StartEngine();
	}
	
	public void StartEngine() {
		
	}
	
	public void StartRun(){
		K.engine.thread.isRun = true;
		Log.i("StartRun",thread.getState().name());
		if(thread.getState() == State.TERMINATED){
			thread = new KThread();
			thread.start();
		}
		else{
			thread.start();
		}
	}

	public void initWindow() {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Display display = super.getWindowManager().getDefaultDisplay();
		K.SysW = display.getWidth();
		K.SysH = display.getHeight();
		K.SysRW = K.SysW / K.W;
		K.SysRH = K.SysH / K.H;
		K.matrix.setScale(K.SysRW, K.SysRH);
		K.rect = new Rect(0, 0, (int)K.SysW, (int)K.SysH);
	}

	public void update() {
		K._time += this._rate;
		long _time = K._time;

		this._flashTime = _time - this._last;

		K.elapsed = this._flashTime / 1000;
		if (K.elapsed > this.maxElapsed) {
			K.elapsed = this.maxElapsed;
		}

		KTweener.update();
		if (!this.paused) {
			K.game.update();
		}
		K.game.mainUpdate();
		K.game.render();
		KInput.update();
		
		//Log.i(String.valueOf(K._timeFrame), String.valueOf(this._last));
		this._last = _time;
		K._timeFrame++;
	}

	public Boolean setRun(Boolean value){
		if(this.thread == null) return false;
		this.thread.isRun = value;
		return this.thread.isRun; 
	}
	
	class KThread extends Thread {
		public Boolean isRun = true;
		public void run() {
			isRun = true;
			while (isRun) {
				try {
					Thread.sleep(KEngine.this._rate);
					KEngine.this.update();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		thread.isRun = false;
		K.stage = null;
		super.onDestroy();
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		  if(keyCode == KeyEvent.KEYCODE_BACK){
			  Log.i("onKeyDown","BACK");
			  if(KEYCODE_BACK() == true) return super.onKeyDown(keyCode, event);
			  onDestroy();
			  KEngine.this.finish();
			  android.os.Process.killProcess(android.os.Process.myPid());
		  }else if(keyCode == KeyEvent.KEYCODE_HOME){
			  if(KEYCODE_HOME() == true) return true;
		  }
		return super.onKeyDown(keyCode, event);
	}
	
	public boolean KEYCODE_BACK(){
		return false;
	}
	public boolean KEYCODE_HOME(){
//		Intent MyIntent = new Intent(Intent.ACTION_MAIN);
//		MyIntent.addCategory(Intent.CATEGORY_HOME);
//		startActivity(MyIntent);
		return true;
	}
}
















